package org.yookiee.support.toolkits;

import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.yookiee.support.constants.DataCacheType;
import org.yookiee.support.constants.I18nConstant;
import org.yookiee.support.core.SysI18nAllocate;
import org.yookiee.support.core.datasource.I18nDatasource;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * FileName I18NManualToolKit
 * Package org.yookiee.support.toolkits
 * Description
 * author cpstory
 * create 2023-10-09 17:18
 * version V1.0
 */
@Slf4j
@Component
public class I18NManualToolKit {
    private static I18nDatasource i18nDatasource;

    @Resource
    SpringContextToolkit springContextToolkit;
    @PostConstruct
    public void initDatasource(){
        i18nDatasource = springContextToolkit.getBean(I18nDatasource.class);
    }

    /**
     * 手动进行翻译
     * @param expel
     * @param lang
     * @return
     */
    public static String translate(String expel,String lang){
        if(Strings.isNullOrEmpty(expel)){
            return expel;
        }
        //判断是否为表达式
        if(!expel.startsWith(I18nConstant.I18N_PREFIX) && !expel.endsWith(I18nConstant.I18N_SUFFIX)){
            return expel;
        }
        Map<String,SysI18nAllocate> i18nAllocates = i18nDatasource.getScopeData(DataCacheType.TYPE_JAVA);
        if (i18nAllocates == null || i18nAllocates.isEmpty()){
            log.info("you have not config the language file,please check your language.properties file");
            return expel;
        }
        List<String> params = I18nToolkit.findParams(expel);
        return I18nToolkit.dataProcess(lang, expel, i18nAllocates, params);

    }

}
